<template>
	<view>
		<view class="tabBox">
			<view class="tabLi">
				<view :class="seledTab == 1 ?'seled':''" @click="selTab(1)">
					消息<text v-if="messageCount">({{ messageCount>= 99?'99+':messageCount }})</text>
				</view>
			</view>
			<view class="tabLi">
				<view  :class="seledTab == 2 ?'seled':''"  @click="selTab(2)">
					待办<text v-if="waitDoCount">({{ waitDoCount>= 99?'99+':waitDoCount }})</text>
				</view>
			</view>
		</view>
		

		<view class="messageList" v-show="seledTab == 1 ">
			<view class="allRead" @click="allRead('','all')">
				全部消息设为已读
			</view>
			<view class="messageLi" style="border: none;" @click="jumpList('messageList','driver_message_business')">
				<view class="imgBox">
					<image src="../../static/message1.png" mode=""></image>
					<text class="num" v-if="buesinessNum">
						{{buesinessNum}}
					</text>
				</view>
				<view class="word">
					业务进度提醒
					<p>{{ DriverMessageBusiness }}</p>
				</view>
			</view>
			<view class="messageLi"  @click="jumpList('payList', 'driver_message_capital')">
				<view class="imgBox">
					<image src="../../static/message2.png" mode=""></image>
					<text class="num" v-if="capiatlNum">
						{{capiatlNum}}
					</text>
				</view>
				<view class="word">
					资金变动提醒
					<p>{{ DriverMessageCapital }}</p>
				</view>
			</view>
			<view class="messageLi"  @click="jumpList('certification','driver_message_certification')">
				<view class="imgBox">
					<image src="../../static/message3.png" mode=""></image>
					<text class="num" v-if="certificationNum">
						{{ certificationNum }}
					</text>
				</view>
				<view class="word">
					账号资质提醒
					<p>{{ DriverMessageCertification }}</p>
				</view>
			</view>
		</view>
		<view class="waitListBox" v-show="seledTab == 2 ">
			<mescroll-body ref="mescroll" @init="mescrollInit" @down="downCallback" @up="upCallback" :down="downOption" :up="upOption">
				<block v-for="(item, index) in waitList" :key="index">
					<view class="column-css view-cente" v-if="item.titleType != 13 && item.titleType != 14">
						<view class="headBox">
							<image src="../../static/icon_client.png" mode=""></image>
							<text>{{item.title}}</text>
						</view>
						<view v-if="item.titleType == 1 || item.titleType == 2 || item.titleType == 3">
							<!-- 1-驾驶证到期提醒
							2-身份证到期提醒
							3-道路运输从业资格证到期提醒 -->
							<view class="waitWord">
								<p>{{ item.content }}</p>
								<p>司机姓名： {{item.driverName}}</p>
								<p>有效期： {{item.validityPeriod}}</p>
							</view>
							<view class="waitBtn"  @click="onDetail('personal/driverregistration',item)">
								立即更新>>>
							</view>
						</view>
						<view v-if="item.titleType == 4">
							<!-- 道路运输证到期提醒 -->
							<view class="waitWord">
								<p>{{ item.content }}</p>
								<p>车牌号： {{item.driverName}}</p>
								<p>有效期： {{item.validityPeriod}}</p>
							</view>
							<view class="waitBtn"  @click="onDetail(`vehicle/vehicleinformation?vehicleId=` + item.vehicleId+'&etp_id=' +etpId ,item)">
								立即更新>>>
							</view>
						</view>
						<view v-if="item.titleType == 5">
							<!-- 账户资质提醒 司机-->
							<view class="waitWord">
								<p>{{ item.content }}</p>
								<p>司机姓名： {{item.driverName}}</p>
								<p>申诉状态：<text>已驳回</text></p>
								<p>驳回原因：{{item.reason}}</p>
							</view>
							<view class="waitBtn"    @click="onDetail('personal/driverregistration',item)">
								立即更新>>>
							</view>
						</view>
						<view v-if="item.titleType == 6">
							<!-- 账户资质提醒 车辆-->
							<view class="waitWord">
								<p>{{ item.content }}</p>
								<p>车牌号： {{item.driverName}}</p>
								<p>申诉状态：<text>已驳回</text></p>
								<p>驳回原因：{{item.reason}}</p>
							</view>
							<view class="waitBtn"   @click="onDetail(`vehicle/vehicleinformation?vehicleId=` + item.vehicleId+'&etp_id=' +etpId ,item)">
								立即更新>>>
							</view>
						</view>
						<view v-if="item.titleType == 7">
							<!-- 入住资质提醒 车辆-->
							<view class="waitWord">
								<p>{{ item.content }}</p>
								<p>司机姓名： {{item.driverName}}</p>
								<p>申诉状态：<text>已驳回</text></p>
								<p>驳回原因：{{item.reason}}</p>
								<p>驳回时间：{{item.operationTime}}</p>
							</view>
						</view>
						<view v-if="item.titleType == 8">
							<!-- 入住资质提醒 -->
							<view class="waitWord">
								<p>{{ item.content }}</p>
								<p>司机姓名： {{item.driverName}}</p>
								<p>申诉状态：<text style="color: #2979ff;">已通过</text></p>
								<p>通过时间：{{item.operationTime}}</p>
							</view>
						</view>
						<view v-if="item.titleType == 9">
							<!-- 活体认证 -->
							<view class="waitWord">
								<p>{{ item.content }}</p>
								<p>司机姓名： {{item.driverName}}</p>
								<p>人脸活体认证：<text>未认证</text></p>
							</view>
							<view class="waitBtn" @click="onDetail('personal/personaldocuments',item)">
								立即认证>>>
							</view>
						</view>
						<view v-if="item.titleType == 10">
							<!-- 协议签署状态 -->
							<view class="waitWord">
								<p>{{ item.content }}</p>
								<p>司机姓名： {{item.driverName}}</p>
								<p>协议签署状态：<text>未签署</text></p>
							</view>
							<view class="waitBtn"  @click="onDetail('personal/signinglist',item)">
								前往签署>>>
							</view>
						</view>
						<view v-if="item.titleType == 11">
							<!-- 协议签署状态 -->
							<view class="waitWord">
								<p>{{ item.content }}</p>
								<p>委托方： {{item.driverName}}</p>
								<p>配载单号：<text>{{item.loadingOrderNo}}</text></p>
							</view>
							<view class="waitBtn">
								<view class="waitBotton" @click="operateLoadingOrder(3,item)">
									驳回
								</view>
								<view class="waitBotton"  @click="operateLoadingOrder(4,item)">
									确认作废
								</view>
							</view>
						</view>
					</view>
					
					<sign-item v-else :item="item" @statusChange="initList"></sign-item>
				</block>
			</mescroll-body>
			<view>
				
			</view>
		</view>
	</view>
</template>

<script>
	import MescrollMixin from '@/uni_modules/mescroll-uni/components/mescroll-uni/mescroll-mixins.js';
	import MescrollBody from '@/uni_modules/mescroll-uni/components/mescroll-body/mescroll-body.vue';
	import SignItem from './components/signItem.vue';
	export default {
		mixins: [MescrollMixin], // 使用mixin
		components: {
			MescrollBody,
			SignItem
		},
		data() {
			return {
				etpId:uni.getStorageSync(this.DATA_KEY.etpId),
				messageCount: '',
				waitDoCount: '',
				seledTab: '1',
				driverId: uni.getStorageSync(this.DATA_KEY.userInfo).driverId,
				DriverMessageBusiness:'',
				DriverMessageCapital:'',
				DriverMessageCertification:'',
				buesinessNum: '',
				capiatlNum: '',
				certificationNum: '',
				waitList: [],
				page: 1,
				pageSize: 10,
				mescroll: null, // mescroll实例对象 (此行可删,mixins已默认)
				// 下拉刷新的配置(可选, 绝大部分情况无需配置)
				downOption: {},
				// 上拉加载的配置(可选, 绝大部分情况无需配置)
				upOption: {
					page: {
						num: 0, // 当前页码,默认0,回调之前会加1,即callback(page)会从1开始
						size: 10 // 每页数据的数量,默认10
					},
					noMoreSize: 5, // 配置列表的总数量要大于等于5条才显示'-- END --'的提示
					empty: {
						tip: '暂无相关数据'
					}
				},
				timer: null,
				
				orderNumber: null,
				accessToken: null,
				platformCoId: uni.getStorageSync(this.DATA_KEY.platformCoNo) == '0001' ? '100001' : '100002',
				flag: false
			}
		},
		onLoad: function(options) {
			console.log(options.type)
			if(options.type == 'daiban'){
				this.selTab(2)
			}
		},
		onShow: function() {
			this.getMessage()
			this.onwaybill()
			// console.log(uni.getStorageSync(this.DATA_KEY.userInfo).driverId ,111111111111)
		},
		methods: {
			//待办的方法
			mescrollInit(mescroll) {
				this.mescroll = mescroll;
			},
			/*下拉刷新的回调*/
			downCallback(mescroll) {
				mescroll.resetUpScroll(); // 重置列表为第一页 (自动执行 page.num=1, 再触发upCallback方法 )
			},
			/*上拉加载的回调*/
			upCallback(mescroll) {
				this.page = mescroll.num; // 页码, 默认从1开始
				//联网加载数据
				this.onwaybill(mescroll);
			},
			//获取待办事项
			onwaybill(mescroll) {
				// this.waitList = [];
				// if (mescroll != null) {
				// 	mescroll.endBySize(this.waitList.length, 0);
				// 	setTimeout(() => {
				// 		this.mescroll.endSuccess(this.waitList.length);
				// 	}, 20);
				// }
				// return false
				this.wxRequest({
					url: `${this.API}/driverMessageCertification/queryList`,
					method: 'POST',
					data: {
						"driverId": this.driverId, //'1424331704348180560',//this.driverId ,
						  "pageNum": this.page,
						  "pageSize": this.pageSize,
						  "type":"2",
							etpId: uni.getStorageSync(this.DATA_KEY.etpId)
					},
					title: '加载中...',
					success: res => {
						let list = res.data.data.list;
						if (this.page == 1) {
							this.waitList = [];
						}
						for (let i = 0; i < list.length; i++) {
							this.waitList.push(list[i]);
						}
						if (mescroll != null) {
							mescroll.endBySize(this.waitList.length, res.data.data.total);
							setTimeout(() => {
								this.mescroll.endSuccess(this.waitList.length);
							}, 20);
						}
					}
				})
			},
			//选择tab
			selTab(val){
				if(val == 2){
					this.onwaybill()
				}
				this.seledTab = val
			},
			//获取消息中心展示什么数据
			getMessage(){
				this.wxRequest({
					url: `${this.API}/driverMessageCommon/queryBellNum`,
					method: 'POST',
					data: {  "driverId": this.driverId, "etpId": this.etpId },
					success: res => {
						if(res.data.code == 0){
							let info =  res.data.data
							this.waitDoCount = info.daibanNum
							this.messageCount = info.buesinessNum + info.capiatlNum + info.certificationNum
							this.DriverMessageBusiness = info.DriverMessageBusiness
							this.DriverMessageCapital = info.DriverMessageCapital
							this.DriverMessageCertification = info.DriverMessageCertification
							this.buesinessNum = info.buesinessNum
							this.capiatlNum = info.capiatlNum
							this.certificationNum = info.certificationNum
						}
					}
				})
			},
			//已读
			allRead(id, tableName){
				let data = {
					  "driverId": this.driverId,
					  "tableName": tableName,
					  "id": id,
				}
				this.wxRequest({
					url: `${this.API}/driverMessageCommon/updateMessageStatus`,
					method: 'POST',
					data: data,
					success: res => {
						if(res.data.code == 0){
							uni.showToast({
								title: '全部已读',
								icon: 'none'
							});
							this.getMessage()
						}
					}
				})
			},
			//跳转到对应页面
			jumpList(val,tableName){
				this.allRead('', tableName)
				uni.navigateTo({
					url: `/pages/message/${val}`
				});
			},
			onDetail(val,item){
				this.wxRequest({
					url: `${this.API}/driverMessageCommon/updateMessageStatus`,
					method: 'POST',
					data: {
						"driverId": this.driverId,
						"tableName": 'daibai',
						"id": item.id,
					},
					success: res => {
						if(res.data.code == 0){
							this.getMessage()
							if(item.titleType == 9){
								this.shake()
							}else{
								uni.navigateTo({
									url: `/pages/${val}`
								});
							}
							
						}
					}
				})
			},
			operateLoadingOrder(val,item){
				let that = this
				uni.showModal({
					title: '提示',
					content: val==3?'将驳回当前作废申请，确认吗?':'将作废当前配载单，确认吗?',
					success: function(res) {
						if (res.confirm) {
							that.wxRequest({
								url: `${that.API}/new/loadingOrder/operateLoadingOrder?loadingOrderId=${item.loadingOrderId}&operate=${val}&driverId=${that.driverId}`,
								method: 'POST',
								data: {  
									"loadingOrderId":item.loadingOrderId,
									"operate": val,
									"driverId": that.driverId 
								},
								success: res => {
									uni.showToast({
										title: '操作成功',
										icon: 'none'
									});
									that.page = 1;
									that.onwaybill()
								}
							})
						} else if (res.cancel) {
							console.log('取消');
						}
					}
				});
			},
			shake() {
				console.log(this.timer,"oooooo")
				
			    if (this.timer) {
			        // clearTimeout(this.timer)
					return
			    }
				this.livingcertification()
			    this.timer = setTimeout(() => {
					console.log(888888)
			    }, 3000)
			},
			initList() {
				console.log('aaaaaaaaaaaaaaaaaaa', this.mescroll)
				this.mescroll.resetUpScroll();
			},
			livingcertification() {
					let that = this;
					that.wxRequest({
						url: `${that.API}/driverService/getDriverIdCardInfo`,
						method: 'POST',
						data: {
							driverId: that.driverId,
							platformCoNo: uni.getStorageSync(this.DATA_KEY.platformCoNo)
						},
						title: '加载中...',
						success: res => {
							let accessToken = res.data.data.accessToken; //认证令牌
							this.accessToken = accessToken
							let taxNumber = res.data.data.taxNumber; //平台税号
							let realName = res.data.data.driverName;
							let idCard = res.data.data.idcard;
							let idPhotoUp = res.data.data.idcard1Base64;
							let idPhotoDown = res.data.data.idcard2Base64;
							that.wxRequestData({
								url: `${that.API2}/api/face/getFaceQRCode?access_token=` + accessToken,
								method: 'POST',
								title: '加载中...',
								data: {
									companyTaxNumber: taxNumber,
									realName: realName,
									idCard: idCard,
									idPhotoUp: idPhotoUp,
									idPhotoDown: idPhotoDown,
									callbackUrl: `${that.API}/driverService/isLivingBodyState`
								},
								success: res => {
									console.log("===================>2",res);
									if (res.data.code == 0) {
										let orderNumber = res.data.data.orderNumber;
										this.orderNumber = orderNumber
										let that = this
										that.wxRequest({
											url: `${that.API}/driverService/saveFaceVerifyOrderNumber`,
											method: 'POST',
											data: {
												driverId: that.driverId,
												orderNumber: orderNumber,
												platformCoId: uni.getStorageSync(this.DATA_KEY.platformCoNo) == '0001' ? '100001' : '100002'
											},
											success: res => {
												// #ifdef APP-PLUS
												uni.showModal({
													title: '提示',
													content: '是否去进行实名认证?跳转进行实名认证后需要重新打开当前APP!',
													success: function(res) {
														if (res.confirm) {
															console.log('用户点击确定');
															that.timer = null
															that.flag = true
															plus.share.getServices(
																function(res) {
																	var sweixin = null;
																	console.log(res);
																	for (var i = 0; i < res.length; i++) {
																		var t = res[i];
																		console.log(t);
																		if (t.id == 'weixin') {
																			sweixin = t;
																		}
																	}
																	if (sweixin) {
																		console.log('dddd=', 'a=' + sweixin + '=b');
																		sweixin.launchMiniProgram({
																			// id: 'gh_cec8224b00da',
																			id: 'gh_45eec401ff1c',
																			type: 0, //0-正式版 1.测试版，2，体验版
																			path: 'pages/verify/info/index?id=' + orderNumber
																		});
																	}
																},
																function(res) {
																	console.log(JSON.stringify(res));
																}
															);
														} else if (res.cancel) {
															console.log('用户点击取消');
															that.timer = null
														}
													}
												});
												// #endif
												
												// #ifdef MP-WEIXIN
												uni.showModal({
													title: '提示',
													content: '是否去进行实名认证?',
													success: function(res) {
														if (res.confirm) {
															that.timer = null
															console.log(that.timer)
															
															that.flag = true
															console.log('用户点击确定');
															uni.navigateToMiniProgram({
																// appId: 'wxfd2861dc1c86b3a0',
																appId: 'wxfc1e27e05fdd062b',
																// path: 'pages/verify/info/index?id=' + orderNumber,
																path: 'pages/verify/info/index?id=' + orderNumber,
																envVersion: 'release',
																success(res) {
																	console.log('跳转成功');
																},
																fail(res) {
																	console.log('跳转失败' + JSON.stringify(res));
																}
															});
														} else if (res.cancel) {
															that.timer = null
															console.log(that.timer)
															console.log('用户点击取消');
														}
													}
												});
												// #endif
											}
										});
									}
								}
							});
						}
					});
				// }
			},
		}
	};
</script>

<style lang="scss">
	page {
		background-color: #ffffff;
		height: 100%;
	}
	.tabBox{
		display: flex;
		flex-direction: row;
		padding: 0 40rpx;
		position: fixed;
		top: 0;
		left: 0;
		width: 100%;
		background-color: #ffffff;
		z-index: 1000;
		.tabLi{
			width: 50%;
			text-align: center;
			font-size: 34rpx;
			color: #8b929a;
			
			view{
				padding-bottom: 20rpx;
				border-bottom: 6rpx solid #FFF;
			}
			.seled{
				display: inline-block;
				width: 180rpx;
				color: #2979ff;
				font-size: 34rpx;
				font-weight: 500;
				border-bottom: 6rpx solid #4089ff;
			}
		}
		
	}
	.allRead{
		text-align: right;
		color: #aab3bc;
		padding: 20rpx;
	}
	.row-css {
		display: flex;
		flex-direction: row;
	}

	.column-css {
		display: flex;
		flex-direction: column;
		margin: 20rpx;
		background: #FFF;
		border-radius: 8rpx;
	}

	.view-centet {
		background-image: linear-gradient(#eef5ff, #d9e7ff);
		justify-content: space-between;
		text-align: center;
		padding: 40rpx;
		padding-right: 30rpx;
		color: #2979ff;
	}

	.view-certification {
		border: 3rpx solid #2979ff;
		border-radius: 50rpx;
		padding: 10rpx 10rpx;
		font-size: 28rpx;
	}

	.view-tocertification {
		background: #2979ff;
		border-radius: 50rpx;
		padding: 10rpx 20rpx;
		font-size: 28rpx;
		color: #ffffff;
	}

	.other-list {
		display: flex;
		justify-content: space-between;
		align-items: center;
		color: #333333;
		margin: 20rpx;
		background: #ffffff;
		padding: 30rpx 20rpx;
	}

	.line {
		background-color: #4089ff;
		margin-right: 15rpx;
		height: 30rpx;
		width: 6rpx;
	}
	.messageList{
		padding: 20rpx;
		margin-top: 66rpx;
		.messageLi{
			display: flex;
			padding: 20rpx ;
			border-top: 1rpx solid #c5cfd9;
			color: #000;
			font-size: 34rpx;
			font-weight: 600;
			.word{
				padding-top: 10rpx;
			}
			p{
				font-size: 28rpx;
				color: #8b929a;
				padding-top: 30rpx;
			}
			.imgBox{
				width: 120rpx;
				height: 120rpx;
				margin-right: 40rpx;
				position: relative;
				image{
					width: 100%;
					height: 100%;
				}
				.num{
					position: absolute;
					top: 0;
					right: 5rpx;
					padding: 0 10rpx;
					color: #FFF;
					background: #ff0101;
					border-radius: 20rpx;
				}
			}
			
		}
		
	}
	.waitListBox{
		background-color: #ededed;
		margin-top: 66rpx;
		.column-css{
			padding: 30rpx 30rpx 0 30rpx;
			font-size: 28rpx;
		}
		.headBox{
			display: flex;
			line-height: 60rpx;
			font-size: 34rpx;
			color: #2979ff;
			image{
				width:60rpx;
				height: 60rpx;
				margin-right: 20rpx;
			}
		}
		.waitWord{
			font-size: 32rpx;
			padding-top: 20rpx;
			color: #8b929a;
			padding-bottom: 60rpx;
			border-bottom: 1rpx solid #e3e3e3;
			p{
				line-height: 40rpx;
				margin-bottom: 10rpx;
				text{
					color: #ff0101;
				}
			}
		}
		.waitBtn{
			text-align: center;
			color: #2979ff;
			line-height: 90rpx;
			.waitBotton{
				margin: 20rpx 0;
				width: calc(50% - 1rpx);
				float: left;
				line-height: 50rpx;
			}
			.waitBotton:first-child{
				border-right: 2rpx solid #2979ff;
			}
		}
	}
</style>
